<?php
global $Config;
require_once($Config->WebCMSRoot.'Base/View.php');
require_once($Config->WebCMSRoot.'Models/User.php');

Class BaseControl
{
	var $parameters;
	var $output;
	var $view;
	var $error;
	var $User;

	var $page;
	
	function BaseControl($parameters, &$page)
	{
		//$this->page = &$page;
		$this->parameters = $parameters;
		$this->error = array(0, '');
		
//print('<pre>'.var_export($this, true).'</pre>');exit;
		
		if ($this->ValidateArguments($parameters))
		{
	  		$this->Execute();
		}
		else
		{
			$this->output = '<p><span style="color:#fff;font-weight:bold;padding:2px;background-color:#f00;border:2px solid #800;">Configuration error in module "'.get_class($this).'".</span></p>';
		}
		
		if (!isset($this->view))
		{
			$this->view = new BaseView();
		}
	}
	
	function Execute()
	{
		if (isset($_SESSION['user']))
		{
			$this->User = unserialize(&$_SESSION['user']);
		}

		$this->output = $this->view->output;
	}

	function ValidateArguments($parameters)
	{
	  // abstract method, should be implemented in sub-class
	}
	
	function GetParameter($key, $multiValue=FALSE)
	{
	  if (isset($this->parameters[$key]))
	  {
	    if ($multiValue)
	    {
	      return $this->parameters[$key];
	    }
	    else
	    {
			if (is_array($this->parameters[$key]))
			{
				if (count($this->parameters[$key]) > 0)
				{
					return $this->parameters[$key][0];
				}
			}
			else
			{
				return $this->parameters[$key];
			}
	    }
	  }
	  
	  return '';
	}

	function ParameterExists($key)
	{
		$exists = 0;
		
		$exists = isset($this->parameters[$key]);

		return $exists;
	}
}
?>